package com.ego.user.controller;

import com.ego.auth.entity.UserInfo;
import com.ego.auth.utils.JwtUtils;
import com.ego.common.utils.PageResult;
import com.ego.user.pojo.User;
import com.ego.user.prop.JwtProperties;
import com.ego.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@EnableConfigurationProperties(JwtProperties.class)
public class UserController {


    @Autowired
    private UserService userService;

    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 数据校验,1校验用户名是否可用 2校验手机号是否唯一
     * check/rabbitadmin/1
     *
     * @return
     */
    @GetMapping("check/{data}/{type}")
    public ResponseEntity<Boolean> checkUserData(@PathVariable(value = "data") String data, @PathVariable(value =
            "type") Integer type) {
        Boolean result = userService.checkUserData(data, type);
        return ResponseEntity.ok(result);
    }

    @PostMapping("/send")
    public ResponseEntity<Void> sendSms(String phone) {
        Boolean result = userService.send(phone.trim());
        if (result) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } else {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
    }

    @PostMapping("register")
    public ResponseEntity<Void> register(@Valid User user, @RequestParam("code") String code) {

        try {
            userService.register(user, code);
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @GetMapping("query")
    public ResponseEntity<User> queryUser(
            @RequestParam("username") String username,
            @RequestParam("password") String password
    ) {
        User user = this.userService.queryUser(username, password);
        if (user == null) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
        }
        return ResponseEntity.ok(user);
    }


    @GetMapping("/{token}")
    public ResponseEntity<User> getUser(@PathVariable("token") String token) {
        UserInfo userInfo;
        User user = null;
        try {
            userInfo = JwtUtils.getInfoFromToken(token, jwtProperties.getPublicKey());
            user = this.userService.getUserByUserInfo(userInfo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (user != null) {
            return ResponseEntity.ok(user);
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).build();
    }

    @GetMapping("/page")
    public ResponseEntity<PageResult<User>> listPage(
            @RequestParam(value = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
            @RequestParam(value = "sortBy", required = false) String sortBy,
            @RequestParam(value = "descending", defaultValue = "false") Boolean descending,
            @RequestParam(value = "key", required = false) String key
    ) {
        PageResult<User> pageResult = userService.getPageResult(pageNo, pageSize, sortBy, descending, key);
        if (pageResult.getItems() == null) {
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
        } else {
            return ResponseEntity.ok(pageResult);
        }

    }

}
